<template>
  <div class="order">
    <div class="tab-list flex-a">
      <div :class="tab == item.value ? 'active item' : 'item'" v-for="item in tabList" :key="item.value"
        @click="changeTab(item.value)">
        <span>{{ item.name }}</span>
        <i></i>
      </div>
    </div>
    <div class="box">
      <el-form :model="queryParams" ref="queryForm" class="input" size="small" :inline="true" v-show="showSearch"
        label-width="68px">
        <el-form-item prop="cashier">
          <el-select v-model="queryParams.cashier" placeholder="请选择收银员">
            <el-option v-for="item in cashierList" :key="item.id" :label="item.name" :value="item.id + ''">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="cashier">
          <el-select v-model="queryParams.type" placeholder="请选择会员类型">
            <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id + ''">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="queryParams.vipName" style="width:340px" prefix-icon="el-icon-search"
            placeholder="请输入订单号/用户姓名/联系电话/备注" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-date-picker v-model="queryParams.time" value-format="yyyy-MM-dd" type="datetimerange"
            :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
            align="right">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" size="mini" @click="handleQuery">查询</el-button>
          <el-button type="warning" class="btn" size="mini" @click="handleExport">导出</el-button>
        </el-form-item>
      </el-form>
      <!-- <div class="stock-total">实付金额：<span>￥0</span> (会员实付￥0，散客实付￥0) <span class="bank"></span> 会员卡消费：<span>￥0</span>
        <span class="bank"></span>
        <el-tooltip class="item" effect="dark" content="次卡消费指统计周期内通过次卡消费抵扣的金额，其中单次次卡消费 = 次卡购卡金额/充值次数" placement="top">
          <i class="el-icon-question"></i>
        </el-tooltip>次卡消费：<span>￥0</span> 
        <span class="bank"></span>
        <el-tooltip class="item" effect="dark"
          content="有效订单不含已撤单及全部退款订单" placement="top">
          <i class="el-icon-question"></i>
        </el-tooltip>有效订单数：<span>0</span>
      </div> -->
      <el-table v-loading="loading" :data="orderList">
        <el-table-column label="订单号" align="center" prop="orderNo">
          <template slot-scope="scope">
            <span @click="orderDetail(scope.row)">{{ scope.row.orderNo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户姓名" align="center" prop="vipName" />
        <el-table-column label="联系电话" align="center" prop="vipPhone" />
        <el-table-column label="实付金额" align="center" prop="price" />
        <el-table-column label="消费时间" align="center" prop="createTime" />
        <el-table-column label="支付方式" align="center" prop="type">
          <template slot-scope="scope">
            {{ scope.row.type=='wx'?'微信':scope.row.type=='zfb'?'支付宝':scope.row.type=='ka'?'会员卡':scope.row.type=='xj'?'现金':'' }}
          </template>
        </el-table-column>
        <el-table-column label="退款金额" align="center" prop="outPrice" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['pet:order:edit']">详情</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['pet:order:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />

      <!-- 添加或修改订单详情对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="订单编号" prop="orderNo">
            <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
          </el-form-item>
          <el-form-item label="会员编号" prop="vipNo">
            <el-input v-model="form.vipNo" placeholder="请输入会员编号" />
          </el-form-item>
          <el-form-item label="会员名称" prop="vipName">
            <el-input v-model="form.vipName" placeholder="请输入会员名称" />
          </el-form-item>
          <el-form-item label="会员电话" prop="vipPhone">
            <el-input v-model="form.vipPhone" placeholder="请输入会员电话" />
          </el-form-item>
          <el-form-item label="收银员" prop="cashier">
            <el-input v-model="form.cashier" placeholder="请输入收银员" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <!-- 订单详情弹窗 -->
      <el-dialog title="订单详情" :visible.sync="detail" width="1000px" append-to-body>
        <el-table :data="orderDetailList">
          <el-table-column label="商品图片" align="center" prop="prodName">
            <template slot-scope="scope">
              <img :src="scope.row.url" style="width: 80px;" alt="">
            </template>
          </el-table-column>
          <el-table-column label="消费项目" align="center" prop="prodName">
            <template slot-scope="scope">
              <span>{{ scope.row.prodName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="零售价" align="center" prop="retPrice" />
          <el-table-column label="折扣率" align="center" prop="discount" />
          <el-table-column label="折后价" align="center" prop="actPrice" />
          <el-table-column label="数量" align="center" prop="num" />
          <el-table-column label="小计" align="center" prop="subtotal" />
          <el-table-column label="实收金额" align="center" prop="subtotal" />
          <el-table-column label="销售员" align="center" prop="cashier" />
          <el-table-column label="选择宠物" align="center" prop="pet" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-delete" v-if="scope.row.actPrice!='已退货'" @click="returnGoods(scope.row)">退货</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination :total="detailTotal" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"/>
        <!-- <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitDetail">确 定</el-button>
          <el-button @click="detail=false">取 消</el-button>
        </div> -->
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder,refOrder } from "@/api/pet/order";

export default {
  name: "Order",
  data() {
    return {
      tab: 'sale',
      detail: false,
      orderDetailList:[],
      tabList: [
        {
          name: '销售订单',
          value: 'sale'
        },
      ],
      detailTotal:0,
      cashierList: [],
      typeList: [
        {
          id: 1,
          name: '会员'
        },
        {
          id: 2,
          name: '散客'
        }
      ],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单详情表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        vipNo: null,
        vipName: null,
        vipPhone: null,
        cashier: null,
        updateUser: null,
        createUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // tab切换
    changeTab(value) {
      this.tab = value
    },
    /** 查询订单详情列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //退货
    returnGoods(row){
      refOrder({id:row.id}).then(res=>{
        if(res.code==200){
          this.$modal.msgSuccess("退货成功！");
          this.detail=false
          this.getList()
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        vipNo: null,
        vipName: null,
        vipPhone: null,
        cashier: null,
        updateUser: null,
        updateTime: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    // 订单详情
    orderDetail(row) {
      this.$router.push({ path: '/business/orderspc' })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getOrder({orderNo:row.orderNo}).then(response => {
        this.detail = true;
        this.orderDetailList=response.rows
        this.detailTotal=response.total
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单详情编号为"' + ids + '"的数据项？').then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pet/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
